package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.OrderConstants;
import cn.itsource.ymcc.constant.PayConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.mapper.PayOrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "PayConsumer",
        topic = PayConstants.TOPIC_PAY_ORDER_DELAY_MESSAGE,
        selectorExpression = PayConstants.TAGS_PAY_ORDER_DELAY_MESSAGE
)
public class PayConsumer  implements RocketMQListener<MessageExt> {
    @Autowired
    private PayOrderMapper payOrderMapper;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String orderNo = new String(body, Constants.UTF8);
            PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>()
                    .eq(PayOrder::getOrderNo, orderNo)
                    .eq(PayOrder::getPayStatus, RConst.Num.BASE_LONG_0));
            if(payOrder.getPayStatus() != null){
                payOrder.setPayStatus(RConst.Num.INT_2);
                payOrderMapper.updateById(payOrder);
                //通知支付宝关闭交易
                System.out.println("支付宝关闭交易");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
